package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.service.ICommonService;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.entity.AgencyExt;
import com.boss.salary.entity.AgencyInfo;
import com.boss.salary.service.IAgencyInfoAuditService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/agencyInfoAuditController")
public class AgencyInfoAuditController extends BasicinfoBaseController {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(AgencyInfoAuditController.class);

    @Autowired
    private IAgencyInfoAuditService agencyInfoAuditService;

    @Autowired
    private ICommonService iCommonService;

    /**
     * 查询表格数据
     * @param limit
     * @param flag
     * @return
     */
    @GetMapping("/queryAgencyInfoPage")
    public ApiResultDTO queryAgencyInfoPage(@RequestHeader("X-Token") String tokenid,
                                            @RequestParam(value = "page") String page,
                                            @RequestParam(value = "limit") String limit,
                                            @RequestParam(value = "flag") String flag,
                                            @RequestParam(value = "agencyId") String agencyId){
        try {
            // 获取下级单位id
            UserDTO userDTO = getUser(tokenid);
            List<String> childrenId = iCommonService.getAgencyChildrenId(userDTO, agencyId,tokenid);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            Map<String, Object> queryParam = new HashMap<>(1);
            queryParam.put("flag", flag);
            PageDTO<AgencyInfo> agencyInfoPageDTO = agencyInfoAuditService.queryAgencyInfoPage(paginationDTO, queryParam, childrenId);
            return ApiResultDTO.success(agencyInfoPageDTO,null);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "查询数据异常");
        }
    }

    /**
     * 审核
     * @return
     */
    @PostMapping("/auditAgencyInfo")
    public ApiResultDTO auditAgencyInfo(@RequestBody List<AgencyInfo> agencyInfoList){
        try{
            agencyInfoAuditService.auditAgencyInfo(agencyInfoList);
            return ApiResultDTO.success();
        }catch (Exception e){
            logger.error("审核发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "审核发生异常");
        }
    }

    /**
     * 撤销审核
     * @return
     */
    @PostMapping("/revokeAudit")
    public ApiResultDTO revokeAudit(@RequestBody List<AgencyInfo> agencyInfoList){
        try{
            agencyInfoAuditService.revokeAudit(agencyInfoList);
            return ApiResultDTO.success();
        }catch (Exception e){
            logger.error("撤销审核发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "撤销审核发生异常");
        }
    }

    /**
     * 退回
     * @return
     */
    @PostMapping("/returnAgencyInfo")
    public ApiResultDTO returnAgencyInfo(@RequestBody List<AgencyInfo> agencyInfoList){
        try{
            agencyInfoAuditService.returnAgencyInfo(agencyInfoList);
            return ApiResultDTO.success();
        }catch (Exception e){
            logger.error("退回发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE,"退回发生异常");
        }
    }

    /**
     * 撤销退回
     * @return
     */
    @PostMapping("/revokeReturn/{id}")
    public ApiResultDTO revokeReturn(@PathVariable("id") String id){
        //        todo 目前只是修改了状态
        AgencyExt entity = new AgencyExt();
        entity.setAgencyId(id);
        entity.setVerifyState(SystemDictConstant.IS_SUBMIT);
        try{
            agencyInfoAuditService.updateAgencyInfoState(entity);
            return ApiResultDTO.success();
        }catch (Exception e){
            logger.error("撤销退回发生异常", e);
            return ApiResultDTO.error(ERROR_RSCODE, "撤销退回发生异常");
        }
    }


}
